<?

define( "ANG", "anglican" );
define( "CLAS", "classical" );

class Smutty_Utils {


	/**
	 *  returns the base URL for the appliation
	 *
	 *  @param String app base url
	 *
	 */

	function getBaseUrl() {
		static $baseUrl;
		if ( $baseUrl == null )
			$baseUrl = substr( $_SERVER['PHP_SELF'], 0,
				strpos($_SERVER['PHP_SELF'], '/index.php') );
		return $baseUrl;
	}

	/**
	 *  gets the url for a controller/action/id.  it's assumed that
	 *  the controller is the current controller unless you specify
	 *  otherwise.
	 *
	 *  this tries to make the url extra pretty by stripping off anything
	 *  on the end of the url that isn't needed (default values, etc...)
	 *
	 *  @param [params] hash of params
	 *  @return String url created
	 *
	 */

	function getUrl( $params ) {

		$router = Smutty_Router::getInstance();
		// if a controller wan't specified then assume the
		// user meant to use the current controller.
		if ( !$params['controller'] )
			$params['controller'] = strtolower($router->getControllerName());

		$url = '';
		$spec = $router->getRouteSpec(
			$params['controller'],
			$params['action']
		);

		$parts = array_reverse(explode( '/', $spec ));
		$valueAdded = false;
		foreach ( $parts as $name ) {

			switch ( $name ) {
				// strip default values for the special params
				// if we haven't added any values yet.
				case 'controller':
				case 'action':
					$value = $params[$name];
					$defName = 'def' . ucfirst($name);
					$default = $router->$defName;
					if ( $name == 'controller' ) {
						$default = strtolower( $default );
						$value = strtolower( $value );
					}
					if ( ($value == $default) && !$valueAdded )
						$value = '';
					break;
				default:
					$value = $params[$name];
			}

			$url = $value . ( $valueAdded ? "/$url" : '' );
			if ( $value ) $valueAdded = true;

		}

		return Smutty_Utils::getBaseUrl() . '/' . preg_replace( '/\/\//', '/', $url );

	}

	/**
	 *  this function turns a commer seperated string
	 *  into a hash (associative array).  it treats the
	 *  string as having commer seperated values like...
	 *
	 *    name,value,name,value
	 *
	 *  @param [str] the string to convert
	 *  @return array
	 *
	 */

	function strToHash( $str ) {

		$hash = array();
		$args = explode( ',', $str );

		for ( $i=0; $i<sizeof($args); $i+=2 )
			$hash[ $args[$i] ] = $args[ $i + 1 ];

		return $hash;

	}

	/**
	 *  this function returns the smutty url (ie. with any base
	 *  url and query string stipped off)
	 *
	 *  @return String
	 *
	 */

	function getSmuttyUrl() {

		$baseUrl = Smutty_Utils::getBaseUrl();
		// strip base url
		$url = substr( $_SERVER['REQUEST_URI'], strlen($baseUrl) + 1 );
		// strip query string
		$url = preg_replace( '/^(.*)\?.*$/', '$1', $url );

		return $url;

	}

	/**
	 *  returns a unique 10 char id string
	 *
	 *  @return string
	 *
	 */

	function getUniqueId() {
		$id = '';
		for ( $i=0; $i<10; $i++ )
			$id .= chr( rand(65,90) );
		return $id;
	}

	/**
	 *  makes a copy of an assoc array and returns it
	 *
	 *  @param [from] the assoc array to copy
	 *  @return array
	 *
	 */

	function copyAssoc( $from ) {
		$array = array();
		foreach ( $from as $key => $value )
			$array[ $key ] = $value;
		return $array;
	}

	/**
	 *  code for this function taken from:
	 *  http://farmproject.object-farm.org/blog/?p=9
	 *
	 *  @param [phrase] the [phrase to pluralise
	 *  @return String the pluralised phrase
	 *
	 */

	function pluralize( $phrase ) {

		$mode = CLAS;

		// default settings
		if(!$phrase || strlen($phrase) < 2) return;

		$suffix_five = substr($phrase,-5,5);
		$suffix_four = substr($phrase,-4,4);
		$suffix_three = substr($phrase,-3,3);
		$suffix_two = substr($phrase,-2,2);
		$suffix_one = substr($phrase,-1,1);

		/* ---------------------------------------- */
		/* Check for nouns which need no inflection */
		
		// -sheep
		if(eregi("sheep",$suffix_five)) return $phrase;
		
		// -deer
		if(eregi("deer",$suffix_four)) return $phrase;
		
		// -itis
		if(eregi("itis",$suffix_four)) return $phrase;
		
		// -fish
		if(eregi("fish",$suffix_four)) return $phrase;
		
		// -ese (e.g. cheese)
		if(eregi("ese",$suffix_three)) return $phrase;
		
		// -ois
		if(eregi("ois",$suffix_three)) return $phrase;
		
		// -pox
		if(eregi("pox",$suffix_three)) return $phrase;
	
		/* ----------------------------------------- */
		/* Check for nouns with irregular inflection */
        
	    // -goose
        if(eregi("goose",$suffix_five)) return substr($phrase,0,strlen($phrase)-5) . "geese";
   
        // [lm]ouse
        if(preg_match("/(^([lm])ouse$)/i",$suffix_five))
        {
			return preg_replace("/([lm])ouse$/i","\\1ice",$phrase);
		}
        
	    // -tooth
        if(eregi("tooth",$suffix_five) || eregi("tooths",$suffix_five)) return substr($phrase,0,strlen($phrase)-5) . "teeth";

        // -foot
        if(eregi("foot",$suffix_four)) return substr($phrase,0,strlen($phrase)-4) . "feet";

        // -zoon
        if(eregi("zoon",$suffix_three)) return substr($phrase,0,strlen($phrase)-4) . "zoa";
        
        // [csx]is
        if(preg_match("/(^([csx])is$)/i",$suffix_three))
        {
			return preg_replace("/(^([csx])is$)/i","\\1es",$phrase);
		}
		
		// potato
		if(eregi("potato",$phrase))
		
		/* ---------------------------------------------- */
		/* Check for fully assimilated classical suffixes */
        
        // -ex
        if(eregi("ex",$suffix_two)) 
        {
			return ($mode == CLAS) ? substr($phrase,0,strlen($phrase)-2) . "ices" : $phrase . "es";
		}
        // -um
        if(eregi("um",$suffix_two)) return substr($phrase,0,strlen($phrase)-2) . "a";
        
        // -on
        if(eregi("on",$suffix_two)) 
        {
			return ($mode == CLAS) ? substr($phrase,0,strlen($phrase)-2) . "a" : $phrase . "s";
		}
        
        // -a
        if(eregi("a",$suffix_one)) 
        {
			return ($mode == CLAS) ? substr($phrase,0,strlen($phrase)-1) . "ae" : $phrase . "s";
		}
    
        /* -------------------------------------------------- */
        /* Check for classical variants of modern inflections */
        
		if($mode == CLAS)
		{
			// -trix
			if(eregi("trix",$suffix_four)) return substr($phrase,0,strlen($phrase)-4) . "trices";

			// -eau
			if(eregi("eau",$suffix_three)) return substr($phrase,0,strlen($phrase)-3) . "eaux";

			// -ieu
			if(eregi("ieu",$suffix_three)) return substr($phrase,0,strlen($phrase)-3) . "ieux";

			// -nx
			if(eregi("nx",$suffix_two)) return substr($phrase,0,strlen($phrase)-2) . "nges";

			// -a
			if(eregi("a",$suffix_one)) return substr($phrase,0,strlen($phrase)-1) . "ata";

			// -is
			if(eregi("is",$suffix_two)) return substr($phrase,0,strlen($phrase)-2) . "ides";
	
		}

			// -en
			if(eregi("en",$suffix_two)) 
			{
				$base = substr($phrase,0,strlen($phrase)-2);
				return ($mode == CLAS) ? $base . "ina" : $base . "ens";
			}

			// -us
			if(eregi("us",$suffix_two)) 
			{
				return ($mode == CLAS) ? substr($phrase,0,strlen($phrase)-2) . "i" : $phrase . "es";
			}
        
        /* -------------------------- */
		/* Check for special suffixes */
		
		// -h
		if(eregi("h",$suffix_one)) return substr($phrase,0,strlen($phrase)-1) . "hes";

		// -ss
		if(eregi("ss",$suffix_two)) return substr($phrase,0,strlen($phrase)-2) . "sses";
		
		// -s
		// TODO: really beef this up.. not the most accurate way of doing it, but works for now!
		if(eregi("s",$suffix_one)) return $phrase . "es";
		
		// -x
		if(eregi("x",$suffix_one)) return $phrase . "es";

		/* ----------------------------- */
		/* Check for lives, wolves, etc. */
                
        // -f
        if(preg_match("/^([aeo])lf$/i",$suffix_three))
        {
			return preg_replace("/([aeo])lf$/i","\\1lves",$phrase);
		}        

		// -[^d]eaf
        if(preg_match("/([^d])eaf$/i",$suffix_four))
        {
			return preg_replace("/([^d]ea)f$/i","\\1ves",$phrase);
		}
		
		// -arf
		if(eregi("arf",$suffix_three)) return substr($phrase,0,strlen($phrase)-3) . "arves";		 		

		// -[nlw]ife
        if(preg_match("/^[nlw]ife$/i",$suffix_four))
        {
			return preg_replace("/([nlw])ife$/i","\\1ives",$phrase);
		}  		
		
		/* ---------------- */
		/* Check for the ys */
		
		// -[aeiou]y
        if(preg_match("/^[aeiou]y$/i",$suffix_two))
        {
			return preg_replace("/([aeiou])y$/i","\\1ys",$phrase);
		} 

		/* -[A-Z].*y
        if(preg_match("/^[A-Z].*y$/",$phrase))
        {
			return preg_replace("/^([A-Z].*)y$/","\\1ys",$phrase);
		} */		

		// -y
		if(eregi("y",$suffix_one)) return substr($phrase,0,strlen($phrase)-1) . "ies";

		/* ------------ */
		/* Check for -o */
		/* TODO: add support for -os words! its lassos, not lassoes! */
        if(preg_match("/^[aeiou]o$/",$suffix_two))
        {
			return preg_replace("/([aeiou])o$/","\\1oes",$phrase);
		} 
		
		/* --------------------------------------- */
		/* If we made it this far, we can probably just add an 's' */
		
		return $phrase . 's';
		
	}

}

?>